﻿// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone

using BlazorAdmin.Models;
using BootstrapBlazor.Components;
using BlazorAdmin.Web.Shared.Components;

namespace BlazorAdmin.Web.Shared.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class DialogExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        public static Task ShowAssignmentDialog<TValule>(this DialogService dialogService, string title, List<SelectedItem> items, List<TValule> values, Func<Task<bool>> saveCallback, ToastService? toast)
        {
            List<string> tValues = (from v in values select v.ToString()).ToList();
            return dialogService.ShowDialog<Assignment, SelectedItem, TValule>(title, items, tValues, saveCallback, toast);
        }

        /// <summary>
        /// 弹出菜单分配弹窗
        /// </summary>
        /// <param name="dialogService"></param>
        /// <param name="title">弹窗标题</param>
        /// <param name="menus">当前用户可用所有菜单集合</param>
        /// <param name="values">已分配菜单集合</param>
        /// <param name="saveCallback"></param>
        /// <param name="toast"></param>
        /// <returns></returns>
        public static async Task ShowNavigationDialog<TValule>(this DialogService dialogService, string title, List<Navigation> menus, List<TValule> values, Func<List<string>, Task<bool>> saveCallback, ToastService? toast)
        {
            DialogOption option = new() { Title = title, ShowFooter = false, IsScrolling = true, Class = "modal-dialog-menu" };
            List<string> tValues = (from v in values select v.ToString()).ToList();
            var parameters = new Dictionary<string, object?>()
            {
                [nameof(NavigationTree.Items)] = menus,
                [nameof(NavigationTree.Value)] = tValues,
                [nameof(NavigationTree.OnClose)] = () => option?.CloseDialogAsync(),
                [nameof(NavigationTree.OnSave)] = new Func<List<string>, Task>(async items =>
                {
                    bool result = await saveCallback(items);
                    if (toast != null)
                    {
                        if (result)
                        {
                            await toast.Success("分配操作", "操作成功！");
                        }
                        else
                        {
                            await toast.Error("分配操作", "操作失败，请联系相关管理员！");
                        }
                    }
                })
            };
            option.Component = BootstrapDynamicComponent.CreateComponent<NavigationTree>(parameters);
            await dialogService.Show(option);
        }

        private static Task ShowDialog<TBody, TItem, TValue>(this DialogService dialogService, string title, List<TItem> items, List<string> values, Func<Task<bool>> saveCallback, ToastService? toast)
            where TBody : AssignmentBase<TItem>
        {
            return dialogService.ShowSaveDialog<TBody>(title, async () =>
            {
                var ret = await saveCallback();
                if (toast != null)
                {
                    if (ret)
                    {
                        await toast.Success("分配操作", "操作成功！");
                    }
                    else
                    {
                        await toast.Error("分配操作", "操作失败，请联系相关管理员！");
                    }
                }
                return ret;
            },
            parameters =>
            {
                parameters.Add(nameof(AssignmentBase<TItem>.Items), items);
                parameters.Add(nameof(AssignmentBase<TItem>.Value), values);
                parameters.Add(nameof(AssignmentBase<TItem>.OnValueChanged), new Action<List<string>>(v =>
                {
                    values.Clear();
                    values.AddRange(v);
                }));
            },
            op => { op.IsScrolling = true; op.Size = Size.ExtraLarge; });
        }
    }
}